"""
子类
MutableSequence(可变序列):
    __setitem__
    __delitem__
    insert
    append
    reverse
    extend
    pop
    remove
    __iadd__
子类
Sequence(不可变序列):
    __getitem__
    __contains__
    __iter__
    __reversed__
    index
    count

超类
__contains__
__iter__
__len__

举例测试可变序列和不可变序列的部分方法
"""


class DictSequence:
    def __init__(self, dict_sequence):
        self.dict_sequence = dict_sequence

    def setitem(self, key, data):
        self.dict_sequence.__setitem__(key, data)
        print(self.dict_sequence)

    def getitem(self, key):
        print(self.dict_sequence.__getitem__(key))

    def use_pop(self, key):
        value = self.dict_sequence.pop(key)
        print(self.dict_sequence, value)


class ListSequence:
    def __init__(self, list_sequence):
        self.list_sequence = list_sequence

    def use_append(self, data):
        self.list_sequence.append(data)
        print(self.list_sequence)

    def use_reverse(self):
        self.list_sequence.reverse()
        print(self.list_sequence)

    def use_extend(self, list_data):
        self.list_sequence.extend(list_data)
        print(self.list_sequence)

    def use_pop(self, index):
        element = self.list_sequence.pop(index)
        # element = self.list_sequence.pop() 会弹出最后一个元素
        print(self.list_sequence, element)

    def use_remove(self, element):
        self.list_sequence.remove(element)
        print(self.list_sequence)

    def use_add(self, data):
        # 相加后返回，加数不变
        res = self.list_sequence.__add__(data)
        print(self.list_sequence, res)

    def use_iadd(self, data):
        # 自增后并返回，加数改变
        res = self.list_sequence.__iadd__(data)
        print(self.list_sequence, res)


class TupleSequence:
    def __init__(self, tuple_sequence):
        self.tuple_sequence = tuple_sequence

    def contains(self, element):
        print(self.tuple_sequence.__contains__(element))

    def use_iter(self):
        tuple_iter = self.tuple_sequence.__iter__()
        print(tuple_iter)           # 迭代器
        # print(next(tuple_iter))    用next()调用一次迭代器会返回一个数据
        # print(next(tuple_iter))
        # print(next(tuple_iter))
        # print(next(tuple_iter)) 超出序列index会抛出StopIteration异常
        return self.tuple_sequence.__iter__()


if __name__ == '__main__':
    print("------dict------")
    ds = DictSequence({})
    ds.setitem('a',1)
    ds.getitem('a')
    ds.use_pop('a')
    print("------list------")
    ls = ListSequence([])
    ls.use_append(1)
    ls.use_append(2)
    ls.use_append(3)
    ls.use_reverse()
    ls.use_extend(['a', 'b', 'c'])
    ls.use_pop(0)
    ls.use_remove('a')
    ls.use_add(['+', '+', '+'])
    ls.use_iadd(['+', '+', '+'])
    print("------tuple------")
    ts = TupleSequence((1, 2, 3))
    ts.contains(1)
    tuple_iter_for = ts.use_iter()

    print("使用for循环遍历迭代器")
    for ele in tuple_iter_for:
        # 使用for遍历迭代器
        print(ele)
    # 或者:
    tuple_iter_while = ts.use_iter()
    print("使用while循环遍历迭代器")

    while True:
        try:
            print(next(tuple_iter_while))
        except StopIteration:
            break
